home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / Math / modf.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  2KB  |  86 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "pml.h"
  4.  
  5. /*
  6.  * Modf returns the positive fractional part of "value" and stores the
  7.  * integer part indirectly through "iptr". Thus the argument "value" and the
  8.  * returned values "modf" and "*iptr" would satisfy the relation
  9.  * 
  10.  *          (*iptr + modf) = value          and 
  11.  *          0 <= modf < abs(value)
  12.  * 
  13.  * For the mc68000 using IEEE format the double precision word format is:
  14.  * 
  15.  * WORD N   =>    SEEEEEEEEEEEMMMM 
  16.  * WORD N+1 =>    MMMMMMMMMMMMMMMM 
  17.  * WORD N+2 =>    MMMMMMMMMMMMMMMM 
  18.  * WORD N+3 =>    MMMMMMMMMMMMMMMM
  19.  * 
  20.  * Where:          S  =>   Sign bit 
  21.  *                 E  =>   Exponent 
  22.  *                 X  =>   Ignored (set to 0)
  23.  *                 M  =>   Mantissa bit
  24.  * 
  25.  * NOTE:  Beware of 0.0; on some machines which use excess 128 notation for the
  26.  * exponent, if the mantissa is zero the exponent is also.
  27.  * 
  28.  */
  29.  
  30. #define MANT_MASK 0x800FFFFF    /* Mantissa extraction mask     */
  31. #define ZPOS_MASK 0x3FF00000    /* Positive # mask for exp = 0  */
  32. #define ZNEG_MASK 0x3FF00000    /* Negative # mask for exp = 0  */
  33.  
  34. #define EXP_MASK 0x7FF00000 /* Mask for exponent            */
  35. #define EXP_SHIFTS 20       /* Shifts to get into LSB's     */
  36. #define EXP_BIAS 1023       /* Exponent bias                */
  37.  
  38. #define W1_FBITS  21        /* (NOTE HIDDEN BIT NORMALIZATION)      */
  39. #define W2_FBITS  32        /* Number of fractional bits in word 2  */
  40. #define WORD_MASK 0xFFFFFFFF    /* Mask for each long word of double    */
  41.  
  42. union dtol {
  43.     double          dval;
  44.     int             ival[2];
  45. };
  46.  
  47. double
  48. modf(value, dptr)
  49.     double          value, *dptr;
  50. {
  51.     double          frac;
  52.     union dtol      number;
  53.     int            *iptr, cexp, fbits;
  54.  
  55.     if (value == 0.0) {
  56.         *dptr = 0.0;
  57.         return (0.0);
  58.     }
  59.     else {
  60.         frac = frexp(value, &cexp);
  61.         number.dval = value;
  62.         iptr = &number.ival[0];
  63.  
  64.         if (cexp <= 0) {
  65.             *iptr++ = 0;
  66.             *iptr++ = 0;
  67.         }
  68.         else {
  69.             if (cexp <= W1_FBITS) {
  70.                 *iptr++ &= (WORD_MASK << (W1_FBITS - cexp));
  71.                 *iptr++ = 0;
  72.             }
  73.             else if (cexp <= (fbits = W1_FBITS + W2_FBITS)) {
  74.                 iptr++;
  75.                 *iptr++ &= (WORD_MASK << (fbits - cexp));
  76.             }
  77.             else {
  78.                 /* pmlerr(DINT_2BIG); */
  79.             }
  80.         }
  81.         *dptr = number.dval;
  82.         frac = value - number.dval;
  83.         return (frac);
  84.     }
  85. }
  86.